﻿using SHomeWorkshop.LunarPresentation.Interfaces;

namespace SHomeWorkshop.LunarPresentation.Commands
{
    public partial class Command
    {
        /// <summary>
        /// ①选择首子面板或选择子面板中第一个公式。
        /// ②或者，如果当前公式是文本块、上下标式、图片框式，则分别：
        ///        编辑文本、编辑上下标文本或更改图片。
        /// </summary>
        public static void SelectInOrEdit()
        {
            if (Globals.selectedEditor == null) return;
            if (Globals.selectedEditor.SelectedExpression == null)
            {
                if (Globals.selectedEditor.RootPanel != null)
                {
                    Globals.selectedEditor.SelectedExpression
                        = Globals.selectedEditor.RootPanel;
                }

                return;
            }

            LunarPresentation.Expressions.Expression curEx
                = Globals.selectedEditor.SelectedExpression;

            if (curEx is Expressions.SubPanel)
            {
                Expressions.SubPanel subPanel = curEx as Expressions.SubPanel;
                if (subPanel.BasePanel.Children.Count > 0)
                {
                    if (subPanel.BasePanel.Children[0] is Expressions.Expression)
                    {
                        curEx.Editor.SelectedExpression =
                            subPanel.BasePanel.Children[0] as Expressions.Expression;
                    }
                }
                return;
            }

            //看看是否被折叠起来，如果是被折叠起来，就展开。
            //注意，支持ICollapsed接口的公式里，扩展式是比较特殊的——它不完全折叠。
            if (curEx is ICollapsed)
            {
                if (curEx is Expressions.Expander == false)
                {
                    ICollapsed collapsedEx = curEx as ICollapsed;

                    if (collapsedEx.IsCollapsed)
                    {
                        ModifingItem mi = new ModifingItem();

                        Action actIsCollapsed = new Action(curEx.ExID, -1, curEx.GetType().Name,
                            "IsCollapsed", collapsedEx.IsCollapsed.ToString(),
                            (!collapsedEx.IsCollapsed).ToString());

                        collapsedEx.IsCollapsed = !collapsedEx.IsCollapsed;

                        mi.Add(actIsCollapsed);
                        curEx.Editor.Do(mi);
                        return;
                    }
                }
            }

            // 普通公式，看能否选中首子面板。
            if (curEx.SubPanels != null && curEx.SubPanels.Count > 0)
            {
                if (curEx.FirstSubPanel != null)
                {
                    curEx.Editor.SelectedExpression = curEx.FirstSubPanel;
                    if (curEx.FirstSubPanel.SkipSelect)
                    {
                        SelectInOrEdit();
                    }
                }
            }
            else
            {
                if (curEx is Expressions.BaseText)
                {
                    // 编辑文本块内容。
                    EditBaseText(curEx as Expressions.BaseText);
                }
                else if (curEx is Expressions.MarkText)
                {
                    EditMarkText(curEx);
                }
                else if (curEx is Expressions.RichText)
                {
                    EditRichText(curEx as Expressions.RichText);
                }
                else if (curEx is Expressions.PictureBox)
                {
                    Expressions.PictureBox pic = curEx as Expressions.PictureBox;
                    pic.LoadImageFromFile();
                }
            }
        }

        /// <summary>
        /// 选中当前子面板中第一个公式。
        /// 如果当前公式不是子面板，则取其父面板——这个是主要功能。
        /// </summary>
        public static void SelectFstChild()
        {
            if (Globals.selectedEditor == null ||
                Globals.selectedEditor.SelectedExpression == null) return;
            if (Globals.selectedEditor.SelectedExpression is Expressions.SubPanel)
            {
                //此时功能同Enter
                Expressions.SubPanel sPanel = Globals.selectedEditor.SelectedExpression as Expressions.SubPanel;
                if (sPanel.BasePanel.Children.Count > 0)
                {
                    sPanel.Editor.SelectedExpression = sPanel.BasePanel.Children[0] as Expressions.Expression;
                }
            }
            else
            {
                Expressions.SubPanel sPanel = Globals.selectedEditor.SelectedExpression.ParentPanel;
                if (sPanel != null && sPanel.BasePanel.Children.Count > 0)
                {
                    sPanel.Editor.SelectedExpression = sPanel.BasePanel.Children[0] as Expressions.Expression;
                }
            }
        }

        /// <summary>
        /// 选中当前子面板中最后一个公式。
        /// 如果当前公式不是子面板，则取其父面板——这个是主要功能。
        /// </summary>
        public static void SelectLstChild()
        {
            if (Globals.selectedEditor == null ||
                Globals.selectedEditor.SelectedExpression == null) return;
            if (Globals.selectedEditor.SelectedExpression is Expressions.SubPanel)
            {
                //此时功能同Enter
                Expressions.SubPanel sPanel = Globals.selectedEditor.SelectedExpression as Expressions.SubPanel;
                if (sPanel.BasePanel.Children.Count > 0)
                {
                    int lstIndex = sPanel.BasePanel.Children.Count - 1;
                    sPanel.Editor.SelectedExpression =
                        sPanel.BasePanel.Children[lstIndex] as Expressions.Expression;
                }
            }
            else
            {
                Expressions.SubPanel sPanel = Globals.selectedEditor.SelectedExpression.ParentPanel;
                if (sPanel != null && sPanel.BasePanel.Children.Count > 0)
                {
                    int lstIndex = sPanel.BasePanel.Children.Count - 1;
                    sPanel.Editor.SelectedExpression =
                        sPanel.BasePanel.Children[lstIndex] as Expressions.Expression;
                }
            }
        }
    }
}
